package com.xzs.thinking.producer;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.UUID;

@Component
@EnableScheduling
public class ProducerKafka {
    @Autowired
    private KafkaTemplate kafkaTemplate;
    @Autowired
    private MsgProducer msgProducer;

    /**
     * 定时任务
     */
    @Scheduled(cron = "0/5 * * * * ?")
    public void send(){
        String message = UUID.randomUUID().toString();
        JSONObject json = new JSONObject();
        json.put("key",message);
        json.put("value",message);
        message = json.toString();
        System.out.println("========send=="+message);
        msgProducer.sendMessage("clustertest",message);
//        ListenableFuture future = kafkaTemplate.send("test", message);
//        future.addCallback(o -> System.out.println("send-消息发送成功：" + json.toString()), throwable -> System.out.println("消息发送失败：" + json.toString()));
    }
}
